<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-07-01
 * Time: 09:32
 * 酒店入住操作
 */

namespace app\api\service\order;


use app\api\model\HotelRoomOrder;
use LinCmsTp5\exception\ParameterException;

class RoomOrderCheckSer
{
    /**
     * 1.获取订单信息
     * 2.获取后台信息
     * 3.判断订单与后台信息
     * 4.修改订单
     */

    // 订单信息
    private $order;

    // 后台信息
    private $linUserId;

    function __construct($orderId,$linUserId)
    {
        $this->order = HotelRoomOrder::get($orderId);
        $this->linUserId = $linUserId;
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeData();
    }

    // 判断订单与后台信息
    private function judgeData()
    {
        if($this->order['status'] != 2) {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '订单状态错误'
                ]
            ]);
        }
        if($this->order['lin_user_id'] != $this->linUserId) {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '该订单不属于该后台'
                ]
            ]);
        }
        return $this->updateOrder();
    }

    private function updateOrder()
    {
        $hotelRoomOrder = new HotelRoomOrder();
        $hotelRoomOrder->save([
            'status' => 4,
            'check_time' => time()
        ],[
            'id' => $this->order['id']
        ]);
        return writeJson(202, [], '入住成功！');
    }
}